<?php
namespace App\Services\PingAnPay;

use App\Services\PingAnPay\lib\pingan;

/**
 * 平安银行支付查询（对账）
 *
 * @author   Leen
 * @date     2016/03/18
 */

    const SEND_URL = 'https://testebank.sdb.com.cn/corporbank/KH0001.pay';

class Reconciliation {



    /**
     * 单笔订单状态查询
     * @param  string $value [description]
     * @return [type]        [description]
     */
    public function singleOrderQuery($order_id)
    {

        $pingan = new pingan();

		$data = array(
			'masterId' => config('pinganpay.masterId'),
			'orderId' => $order_id,
		);


		$xml_data = $pingan->array_to_xml($data);
        $merchantCertFile = config('pinganpay.merchant_cert_file');

		//获取签名后的orig和sign
		$orig = $pingan->getOrig($xml_data);
		$sign = $pingan->getSign($merchantCertFile, $xml_data);

		//通过curl请求接口
		$parms = 'orig=' . $orig . '&sign=' . $sign;
		$rsponse = $pingan->curl(SEND_URL, $parms);

		//通过字符串截取获取orig
		$rsponseData = explode('orig=', $rsponse);
		$rsponse2Data = explode('SDBPAYGATE=', $rsponseData[1]);

		//解码
		$formOrig = $pingan->_base64_url_decode($rsponse2Data[0]);

		$xml_dom = str_replace("GBK", "utf-8", $formOrig);
		$xml_dom = mb_convert_encoding($xml_dom, 'utf-8', 'gbk');
		$result = $pingan->xml_to_array($xml_dom);

		return $result;
		//输出结果
		/*echo '<?xml version="1.0" encoding="GBK"?>';
		echo $formOrig;
		die('this is gbk!please check result by console');*/
    }


}

